package com.ag.contactTree.action;

import java.util.List;

import javax.annotation.Resource;

import com.ag.bean.TbDeptType;
import com.ag.common.BaseAction;
import com.ag.contactTree.TreeNode;
import com.ag.contactTree.service.LocalTreeService;
import com.ag.dao.TbDeptTypeDAO;
import com.ag.zhh.vo.UserDept;

/**
 * 对于树的action
 * 
 * @author 张颢铧
 * 
 */
public class MedicineTreeAction extends BaseAction {
	// 参数
	private String nodeId;

	private String treeType;

	private String deptType;// general日常、emergency应急，all所有

	private List<TbDeptType> deptTypes;
	/**
	 * 药监内部机构的通讯录 地址为/do/medicineTree_getMedicineTreeRoot.action
	 * forward到zhh/contact.jsp
	 * 
	 * @return
	 */

	/**
	 * 跳到去打电话的界面,访问地址：/ag/do/medicineTree_getMedicineTreeRoot.action
	 * 
	 * @return
	 */
	public String getMedicineTreeRoot() {

		try {
			System.out.println("树的类型："+treeType);
			if (treeType.equals("0000")) {
				this.getRequest().setAttribute("treeRoot",
						localTreeService.getMedicineTreeRoot());// OA通讯录的树
				System.out.println("adfasfasf树的类型："+treeType);
			}
			if (treeType.equals("0001")) {
				this.getRequest().setAttribute("treeRoot",
						localTreeService.getCoordinatTreeRoot());// 监督部门的树
				//this.setNodeId(localTreeService.getCoordinatTreeRoot().getNodeId());
			}
			if (treeType.equals("0002")) {
				this.getRequest().setAttribute("treeRoot",
						localTreeService.getEmergencyTreeRoot());// 应急部门的树
			}
			if (treeType.equals("0003")) {
				this.getRequest().setAttribute("treeRoot",
						localTreeService.getExternalTreeRoot());// 外协单位的树
				
			}
			
			findDeptTypes();
			if (deptType == null || deptType.equals("")) {
				deptType="all";
			}
			this.getRequest().setAttribute("deptType", deptType);
			this.getRequest().setAttribute("treeType", treeType);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "tree";
	}

	/**
	 * 直接获取整个药监内部通讯录树
	 * 
	 * @return
	 */
	public String getMedicineTree() {
		this.getRequest().setAttribute("tree", localTreeService.getMedicineTree());
		return "tree";
	}

	/**
	 * 药监内部机构的通讯录维护 地址/do/tree_mtMedicineTreeRoot.action
	 * forward到zhh/contree/contact.jsp
	 * 
	 * @return
	 */
	// 返回4棵树的树根
	// 跳到去通信录维护的界面的左边
	public String mtMedicineTreeRoot() {
		
		if (treeType.equals("0000")) {
			this.getRequest().setAttribute("treeRoot",
					localTreeService.getMedicineTreeRoot());// OA通讯录的树
		}
		if (treeType.equals("0001")) {
			this.getRequest().setAttribute("treeRoot",
					localTreeService.getCoordinatTreeRoot());// 监督部门的树
			//this.setNodeId(localTreeService.getCoordinatTreeRoot().getNodeId());
		}
		if (treeType.equals("0002")) {
			this.getRequest().setAttribute("treeRoot",
					localTreeService.getEmergencyTreeRoot());// 应急部门的树
		}
		if (treeType.equals("0003")) {
			this.getRequest().setAttribute("treeRoot",
					localTreeService.getExternalTreeRoot());// 外协单位的树
			
		}
		List<TbDeptType> list = localTreeService.findDeptType();
		this.getRequest().setAttribute("depts", list);			//部门类型
		return "maintaintree";
	}

	/**
	 * 药监内部树维护通过nodeId来获取节点的联系人表和子部门表,返回到通信录维护界面的右边
	 * 
	 * @return
	 */
	public String getOAMedicineTreeServiceChildren() {
		System.out.println("-----------------------" + nodeId);
		TreeNode node = null;

		// 查询oa通信录的树
		node = localTreeService.getMedicineTreeNodeById(nodeId);
		this.getRequest().setAttribute("node", node);
		this.getRequest().setAttribute("treeType", treeType);
		System.out.println("MedicineTreeAction: \n\n\n\n树的类型：" + treeType
				+ "\n\n\n\n");
		return "OAtreeChildren";
	}

	// 本地通信录
	public String getLocalMedicineTreeServiceChildren() {
		System.out.println("-----------------------" + nodeId);

		TreeNode node = null;
		if (treeType == null)
			node = localTreeService.getTreeNodeById(nodeId);
		else if (treeType.equals("0000"))
			node = localTreeService.getMedicineTreeNodeById(nodeId);
		else if (treeType.equals("0001"))
			node = localTreeService.getCoordinatTreeNodeById(nodeId);
		else if (treeType.equals("0002"))
			node = localTreeService.getEmergencyTreeNodeById(nodeId);
		else if (treeType.equals("0003"))
			node = localTreeService.getExternalTreeNodeById(nodeId);

		this.getRequest().setAttribute("node", node);
		this.getRequest().setAttribute("treeType", treeType); // 保存树的类型
		System.out.println("MedicineTreeAction: 树节点 ：" + nodeId + " 树的类型："
				+ treeType + "\n\n\n\n");
		UserDept dept = (UserDept)node.getBindData();
		if(dept!=null)
			System.out.println("MedicineTreeAction:   部门id ："+dept.getUdId()+" 部门名："+dept.getUdName()+" 手机："+dept.getUdPhone()+" 电话："+dept.getUdTel()+" 传真："+dept.getUdFax());
		return "Children";
	}

	private void findDeptTypes() {
		deptTypes = localTreeService.findDeptType();
	}



	// 参数ends

	public String getNodeId() {
		return nodeId;
	}

	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}

	/**
	 * undo 修改药监内部通讯录部门通讯录
	 * 
	 * @return
	 */
	public String updateMedicineTreeNode() {

		return "updateMedicineTreeNode";
	}

	// 以下是逻辑类注入和构造函数
	// 树的类型
	private LocalTreeService localTreeService; // 本地通信录的service

	@Resource(name = "TbDeptTypeDAO")
	private TbDeptTypeDAO deptTypedao;

	public MedicineTreeAction() {

	}

	public String getTreeType() {
		return treeType;
	}

	public void setTreeType(String treeType) {
		
		this.treeType = treeType;
		System.out.println("树的类型：treeType-"+this.treeType);
	}

	public LocalTreeService getLocalTreeService() {
		return localTreeService;
	}

	public void setLocalTreeService(LocalTreeService localTreeService) {
		this.localTreeService = localTreeService;
	}

	public String getDeptType() {
		return deptType;
	}

	public void setDeptType(String deptType) {
		this.deptType = deptType;
	}

	public List<TbDeptType> getDeptTypes() {
		return deptTypes;
	}

	public void setDeptTypes(List<TbDeptType> deptTypes) {
		this.deptTypes = deptTypes;
	}

}
